package com.wenhao.leetcode.medium;
import java.util.HashSet;
import java.util.Set;

/**
 * 题目：https://leetcode-cn.com/problems/longest-consecutive-sequence/
 * 最长连续序列
 *
 * @author Wenhao Tong
 * @create 2021-07-12 17:51
 */
public class LeetCode128 {
    public static void main(String[] args) {
        LeetCode128 leetCode128 = new LeetCode128();
        int i = leetCode128.longestConsecutive(new int[]{100, 1, 200, 3, 4, 2});
        System.out.println(i);
    }

    public int longestConsecutive(int[] nums) {
        Set<Integer> s = new HashSet<>();
        for (int i = 0;i < nums.length;i++) {
            s.add(nums[i]);
        }
        int max = 0;
        for (Integer i : s) {
            if (!s.contains(i - 1)) {
                int cur = i;
                int length = 0;
                while (s.contains(cur)) {
                    length++;
                    cur++;
                }
                max = max > length ? max : length;
            }
        }
        return max;
    }
}
